﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using MM.DBUtility;
using MM.Common;

namespace WebMMS.Order
{
    public partial class OrderMgrSZF : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["CurUser"] == null)
            {
                Response.Redirect("~/Login.aspx");
                return;
            }
            if (!IsPostBack)
            {
                inputDateFrom.Value = DateTime.Now.ToString("yyyy-MM-dd");
                inputDateTo.Value = DateTime.Now.ToString("yyyy-MM-dd");

                AspNetPager1.CurrentPageIndex = 1;
                GVDataBind();
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(inputDateFrom.Value) && !string.IsNullOrEmpty(inputDateTo.Value))
                {
                    if (Convert.ToDateTime(inputDateFrom.Value) > Convert.ToDateTime(inputDateTo.Value))
                    {
                        MM.Common.Common.PageStartAlert(this.Page, "请求时间From必须小于等于To！");
                        return;
                    }
                }
                AspNetPager1.CurrentPageIndex = 1;
                GVDataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void GVDataBind()
        {
            string strSQL = string.Format("SELECT a.OrderID,a.UserID,a.Price,a.Status,a.CreateTime,a.PayTime,a.AppID,a.ChargeMode,b.CardNum,b.SzfPrice,b.Op FROM T_Order a,T_OrderSZFDetail b WHERE a.chargeMode=2 AND a.OrderID=b.OrderID ");

            List<SqlParameter> parameters = new List<SqlParameter>();
            if (!ddlStatus.SelectedValue.Equals("-1"))
            {
                strSQL += " AND a.Status = @Status";
                parameters.Add(SqlHelper.CreateInParameter("@Status", SqlDbType.Int, 2, ddlStatus.SelectedValue));
            }
            if (!string.IsNullOrEmpty(txtOrderID.Text.Trim()))
            {
                strSQL += " AND a.OrderID = @OrderID";
                parameters.Add(SqlHelper.CreateInParameter("@OrderID", SqlDbType.Decimal, 18, txtOrderID.Text.Trim()));
            }
            if (!string.IsNullOrEmpty(inputDateFrom.Value) && !string.IsNullOrEmpty(inputDateTo.Value))
            {
                strSQL += " AND a.CreateTime BETWEEN @fromTime AND @toTime ";
                parameters.Add(SqlHelper.CreateInParameter("@fromTime", SqlDbType.DateTime, 25, inputDateFrom.Value));
                parameters.Add(SqlHelper.CreateInParameter("@toTime", SqlDbType.DateTime, 25, Convert.ToDateTime(inputDateTo.Value).AddDays(1).ToString("yyyy-MM-dd")));
            }

            string strOrder = " ORDER BY a.CreateTime DESC";

            AspNetPager1.RecordCount = DbHelperSql.GetSingle("SELECT COUNT(1) FROM (" + strSQL + ") AS B", parameters.ToArray()).ToIntOr();

            if (AspNetPager1.CurrentPageIndex == 1)
                strSQL = string.Format(strSQL.Replace("SELECT *", "SELECT TOP {0} * "), AspNetPager1.PageSize) + strOrder;
            else
                strSQL = string.Format("SELECT TOP {0} * FROM ({1}) AS F WHERE rn BETWEEN {2} AND {3}", AspNetPager1.PageSize, strSQL.Replace("SELECT ", string.Format("SELECT row_number() over({0}) rn, ", strOrder)), (AspNetPager1.CurrentPageIndex - 1) * AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex * AspNetPager1.PageSize - 1);

            DataSet ds = DbHelperSql.Query(strSQL, parameters.ToArray());
            GVinfo.DataSource = ds;
            GVinfo.DataBind();
            lbCount.Text = "记录数：" + AspNetPager1.RecordCount;

        }

        protected void GVinfo_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate))
            {
                Label lbStatus = (Label)e.Row.FindControl("lbStatus");
                if (lbStatus.Text.Trim().Equals("1"))
                    lbStatus.Text = "已支付";
                else if (lbStatus.Text.Trim().Equals("0"))
                    lbStatus.Text = "未支付";

                Label lbOp = (Label)e.Row.FindControl("lbOp");
                if (lbOp.Text.Trim().Equals("1"))
                    lbOp.Text = "移动";
                else if (lbOp.Text.Trim().Equals("2"))
                    lbOp.Text = "联通";
                else if (lbOp.Text.Trim().Equals("3"))
                    lbOp.Text = "电信";
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            GVDataBind();
        }
    }
}